package guda.house.biz.impl;

import java.util.List;

import guda.house.biz.constants.CommonResultCode;
import guda.house.biz.query.*;
import guda.house.dal.dao.*;
import guda.house.dal.dataobject.*;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;

import guda.grape.autogen.common.page.BaseQuery;
import guda.grape.autogen.common.page.BizResult;
import guda.house.biz.ParkingBiz;
import guda.house.biz.enums.SysVarTypeEnum;

public class ParkingBizImpl implements ParkingBiz{

    private final static Logger logger = LoggerFactory.getLogger(ParkingBizImpl.class);

    @Autowired
    private ParkingDAO parkingDAO;

    @Autowired
    private SysVarDAO sysVarDAO;
    @Autowired
    private OwnerDAO ownerDAO;
    @Autowired
    private BuildingDAO buildingDAO;
    @Autowired
    private UnitDAO unitDAO;
    @Autowired
    private SuiteDAO suiteDAO;

    public BizResult detail(long id) {
        BizResult bizResult = new BizResult();
        try{
            ParkingDO parkingDO = parkingDAO.getParkingById(id);
            bizResult.data.put("parkingDO", parkingDO);
            bizResult.success = true;
        }catch(Exception e){
            logger.error("query Parking error",e);
        }
        return bizResult;
}

    public BizResult list(BaseQuery baseQuery) {
        BizResult bizResult = new BizResult();
        try {
            int totalCount = parkingDAO.countForPage(baseQuery);
            baseQuery.setTotalCount(totalCount);
            List<ParkingDO> parkingList = parkingDAO.selectForPage(baseQuery);
            bizResult.data.put("parkingList", parkingList);
            bizResult.data.put("query", baseQuery);
            bizResult.success = true;
        } catch (Exception e) {
            logger.error("view Parking list error", e);
        }
            return bizResult;
     }

    public BizResult delete(long id) {
        BizResult bizResult = new BizResult();
        try {
            parkingDAO.delParkingById(id);
            bizResult.success = true;
        } catch (Exception e) {
            logger.error("delete parking error", e);
        }
        return bizResult;
    }

    public BizResult create(ParkingDO parkingDO) {
        BizResult bizResult = new BizResult();
        try {
            List<Long> longs = parkingDAO.checkRepeat(parkingDO.getParkingName(), parkingDO.getVillageId());
            if(longs.size()>0){
                bizResult.code=CommonResultCode.PARKING_NAME_REPEAT;
                return bizResult;
            }
            SysVarDO sysVarById = sysVarDAO.getSysVarById(parkingDO.getParkingTypeId());
            if(sysVarById == null){
                bizResult.code = CommonResultCode.RECORD_NOT_FOUND;
                return bizResult;
            }
            parkingDO.setParkingType(sysVarById.getVarValue());
            long id = parkingDAO.insert(parkingDO);
            bizResult.data.put("id", id);
            bizResult.success = true;
        } catch (Exception e) {
            logger.error("create Parking error", e);
        }
        return bizResult;
    }

    @Override
    public BizResult update(ParkingDO parkingDO) {
        BizResult bizResult = new BizResult();
        try {
            List<Long> longs = parkingDAO.checkRepeat(parkingDO.getParkingName(), parkingDO.getVillageId());
            if(longs.size()>1){
                bizResult.code=CommonResultCode.PARKING_NAME_REPEAT;
                return bizResult;
            }else if(longs.size()==1 && longs.get(0).longValue() != parkingDO.getParkingId().longValue()){
                bizResult.code=CommonResultCode.PARKING_NAME_REPEAT;
                return bizResult;
            }
            SysVarDO sysVarById = sysVarDAO.getSysVarById(parkingDO.getParkingTypeId());
            if(sysVarById == null){
                bizResult.code = CommonResultCode.RECORD_NOT_FOUND;
                return bizResult;
            }
            parkingDO.setParkingType(sysVarById.getVarValue());
            parkingDAO.updateByIdSelective(parkingDO);
            bizResult.success = true;
        } catch (Exception e) {
            logger.error("update Parking error", e);
        }
        return bizResult;
    }

    @Override
	public BizResult queryForCreate(Long villageId) {
		BizResult bizResult = new BizResult();
		SysVarQuery sysVarQuery = new SysVarQuery();
        sysVarQuery.setVillageId(villageId);
		sysVarQuery.setVarType(SysVarTypeEnum.parking.name());
		sysVarQuery.setPageSize(500);
		List<SysVarDO> sysVarDOs = sysVarDAO.selectForPage(sysVarQuery);
		bizResult.data.put("sysVarList", sysVarDOs);
        //查询所有楼栋
        BuildingQuery buildingQuery = new BuildingQuery();
        buildingQuery.setPageSize(10000);
        buildingQuery.setVillageId(villageId);
        List<BuildingDO> buildingDOs = buildingDAO.selectForPage(buildingQuery);
        bizResult.data.put("buildingList",buildingDOs);
        bizResult.success = true;
		return bizResult;
	}

    @Override
    public BizResult queryForEdit(Long parkngId) {
        BizResult bizResult = new BizResult();



        ParkingDO parkingById = parkingDAO.getParkingById(parkngId);
        SuiteDO suiteById = suiteDAO.getSuiteById(parkingById.getSuiteId());
        UnitDO unitById = unitDAO.getUnitById(suiteById.getUnitId());
        BuildingDO buildingDO = buildingDAO.getBuildingById(unitById.getBuildingId());

        bizResult.data.put("suiteDO", suiteById);
        bizResult.data.put("unitDO", unitById);
        bizResult.data.put("buildingDO", buildingDO);

        OwnerDO ownerById = ownerDAO.getOwnerById(parkingById.getOwnerId());
        bizResult.data.put("ownerDO", ownerById);

        OwnerQuery ownerQuery = new OwnerQuery();
        ownerQuery.setSuiteId(suiteById.getSuiteId());
        List<OwnerDO> ownerDOs = ownerDAO.selectForPage(ownerQuery);
        bizResult.data.put("ownerList", ownerDOs);

        BuildingQuery buildingQuery = new BuildingQuery();
        buildingQuery.setVillageId(buildingDO.getVillageId());
        buildingQuery.setPageSize(10000);
        List<BuildingDO> buildingDOs = buildingDAO.selectForPage(buildingQuery);
        bizResult.data.put("buildingList", buildingDOs);

        UnitQuery unitQuery = new UnitQuery();
        unitQuery.setPageSize(10000);
        unitQuery.setBuildingId(buildingDO.getBuildingId());
        List<UnitDO> unitDOs = unitDAO.selectForPage(unitQuery);
        bizResult.data.put("unitList", unitDOs);

        SuiteQuery suiteQuery = new SuiteQuery();
        suiteQuery.setPageSize(10000);
        suiteQuery.setUnitId(unitById.getUnitId());
        List<SuiteDO> suiteDOs = suiteDAO.selectForPage(suiteQuery);
        bizResult.data.put("suiteList", suiteDOs);


        SysVarQuery sysVarQuery = new SysVarQuery();
        sysVarQuery.setVillageId(parkingById.getVillageId());
        sysVarQuery.setVarType(SysVarTypeEnum.parking.name());
        sysVarQuery.setPageSize(500);
        List<SysVarDO> sysVarDOs = sysVarDAO.selectForPage(sysVarQuery);
        bizResult.data.put("sysVarList", sysVarDOs);
        bizResult.success = true;
        return bizResult;
    }

    @Override
    public BizResult queryForReset(Long villageId, Integer buyed) {
        BizResult bizResult = new BizResult();
        SysVarQuery sysVarQuery = new SysVarQuery();
        if(buyed == null) {
            sysVarQuery.setVarType(SysVarTypeEnum.parking.name());
        }else if(buyed.intValue() == 0){
            sysVarQuery.setVarType(SysVarTypeEnum.parking_rent.name());
        }
        sysVarQuery.setPageSize(500);
        List<SysVarDO> sysVarDOs = sysVarDAO.selectForPage(sysVarQuery);
        bizResult.data.put("sysVarList", sysVarDOs);
        //查询所有楼栋
        BuildingQuery buildingQuery = new BuildingQuery();
        buildingQuery.setPageSize(10000);
        buildingQuery.setVillageId(villageId);
        List<BuildingDO> buildingDOs = buildingDAO.selectForPage(buildingQuery);
        bizResult.data.put("buildingList",buildingDOs);
        bizResult.success = true;
        return bizResult;
    }
}
